﻿/* **********************************************************************************
 *
 * Copyright (c) WickedFlame Projects. All rights reserved.
 *
 * This source code is subject to terms and conditions of the Microsoft Reciprocal License
 * for WickedFlame. A copy of the license can be found in the License.html file
 * at the root of this distribution. 
 * By using this source code in any fashion, you are agreeing to be bound by
 * the terms of the Microsoft Reciprocal License for WickedFlame.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * **********************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Gencode.Controls.CollapsibleControl
{
	public enum CollapsibleItemStrechStyle { Strech, Custom };

	[ToolboxItem(false)]
	public partial class CollapsibleItem : UserControl, IContainerControl
	{
		public CollapsibleItem()
		{
			InitializeComponent();

			Header = new CollapsibleItemHeader("button");
			Header.Height = 17;
			Header.ListHeaderStateChanged += new ListHeaderStateChangedEventHandler(ListHeaderStateChanged);
		}

		public CollapsibleItem(string text)
		{
			InitializeComponent();

			Header = new CollapsibleItemHeader(text);
			Header.Height = 17;
			Header.ListHeaderStateChanged += new ListHeaderStateChangedEventHandler(ListHeaderStateChanged);

			this.Text = text;
		}

        internal void ListHeaderStateChanged(object sender, ListHeaderStateChangedEventArgs e)
        {
            CollapsibleItemHeader header = (CollapsibleItemHeader)sender;

            if (e.State == ListHeaderState.Collapsed)
            {
                this.Hide();
                m_isCollapsed = true;
            }
            else
            {
                this.Show();
                m_isCollapsed = false;
            }
        }

        internal void OnResize(object sender, CollapsibelControlResizeEventArgs e)
        {
			this.Width = e.ParentClientWidth - (this.Margin.Left + this.Margin.Right);

            SetHeight(sender as CollapsibleControl);
        }

        private void SetHeight(CollapsibleControl control)
        {
            if (StrechStyle == CollapsibleItemStrechStyle.Strech)
            {
                int h = 0;
                int strechCount = 0;
                if (!m_isCollapsed)
                {
                    foreach (CollapsibleItem item in control.Nodes)
                    {
                        if (item.StrechStyle == CollapsibleItemStrechStyle.Custom)
                        {
							h += (item.Header.Height + item.Header.Margin.Top + item.Header.Margin.Bottom);
							if (!item.IsCollapsed)
								h += (item.Height + item.Margin.Top + item.Margin.Bottom);
                        }
                        else
                        {
							h += (item.Header.Height + item.Header.Margin.Top + item.Header.Margin.Bottom);
                            if (!item.IsCollapsed)
                                strechCount++;
                        }
                    }
                }
				if (strechCount > 0)
					this.Height = ((control.ClientSize.Height - h) / strechCount) - (this.Margin.Bottom + this.Margin.Top);
            }
        }

        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public override string Text
		{
			get
			{
				if (Header != null)
					return Header.Text;
				return base.Text;
			}
			set
			{
				Header.Text = value;
				base.Text = value;
			}
		}

        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public CollapsibleItemHeader Header
        {
            get;
            set;
        }

        CollapsibleItemStrechStyle m_strechStyle = CollapsibleItemStrechStyle.Custom;
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [DefaultValue(CollapsibleItemStrechStyle.Custom)]
        public CollapsibleItemStrechStyle StrechStyle
        {
            get
            {
                return m_strechStyle;
            }
            set
            {
                m_strechStyle = value;
            }
        }

        bool m_isCollapsed = false;
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [DefaultValue(false)]
        public bool IsCollapsed
        {
            get
            {
                return m_isCollapsed;
            }
        }
	}
}
